<script setup>
import { reactive, toRefs, ref, getCurrentInstance } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { login } from "@/api/login";
import { setLocalData, setToken } from "@/utils/auth";
const { proxy } = getCurrentInstance();
const formRef = ref();
const isFocus = ref(false);
const isFocus1 = ref(false);
const formData = reactive({
  username: "",
  password: "",
});
const rules = reactive({
  username: [
    {
      required: true,
      message: "此项为必填项",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "此项为必填项",
      trigger: "blur",
    },
  ],
});
const router = useRouter();
const handleLogin = () => {
  if (!formRef) return;
  //   console.log('formRef: ', formRef);
  formRef.value.validate(async (valid, fields) => {
    if (valid) {
      //   console.log("submit!",formData);
      const res = await login(formData);
      //   console.log("res: ", res);
      setToken(res.token);
      setLocalData(res.user);
      proxy.$message.success(res.msg);
      router.push({
        path: "/layout",
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
const handlerFocus = () => {
  isFocus.value = true;
  // console.log("聚焦");
};
const handlerBlur = () => {
  // console.log("失焦");
  isFocus.value = false;
};
const handlerFocus1 = () => {
  isFocus1.value = true;
  // console.log("聚焦");
};
const handlerBlur1 = () => {
  // console.log("失焦");
  isFocus1.value = false;
};
</script>

<template>
  <div class="login_wrap">
    <img src="@/assets/bg.png" alt="" class="wave" />
    <div class="container">
      <div class="img">
        <img src="@/assets/img-3.svg" alt="" />
      </div>
      <div class="login-box">
        <img src="@/assets/avatar.svg" alt="" class="avatar" />
        <h2>Welcome</h2>
        <el-form
          ref="formRef"
          label-width="100px"
          class="demo-dynamic"
          :rules="rules"
          :model="formData"
        >
          <div class="input-group" :class="isFocus ? 'focus' : ''">
            <div class="icon">
              <i class="fa fa-user"></i>
            </div>
            <div>
              <h5>Username</h5>
              <el-form-item
                prop="username"
                label="用户名"
                class="input nolabel"
              >
                <el-input
                  v-model="formData.username"
                  autocomplete="new-password"
                  readonly
                  v-removeReadonly
                  @focus="handlerFocus"
                  @blur="handlerBlur"
                />
              </el-form-item>
            </div>
          </div>
          <div class="input-group" :class="isFocus1 ? 'focus' : ''">
            <div class="icon">
              <i class="fa fa-lock"></i>
            </div>
            <div>
              <h5>Password</h5>
              <el-form-item prop="password" label="密码" class="input nolabel">
                <el-input
                  v-model="formData.password"
                  type="password"
                  autocomplete="off"
                  @focus="handlerFocus1"
                  @blur="handlerBlur1"
                />
              </el-form-item>
            </div>
          </div>
          <el-button type="primary" class="login_btn btn" @click="handleLogin"
            >登录</el-button
          >
        </el-form>
      </div>
    </div>
    <a href="javascript:void" target="_blank" class="copyright"
      >&copy; 鲜花后台管理系统</a
    >
  </div>
</template>

<style scoped>
/* .login_wrap {
  width: 100%;
  height: 100vh;
  background: rgb(56, 86, 139);
  position: relative;
}
.form_wrap {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  padding: 30px 50px;
  border-radius: 5px;
}
.login_btn {
  display: block;
  margin: 10px auto;
} */
.wave {
  position: fixed;
  height: 100%;
  left: 0;
  bottom: 0;
  z-index: -1;
}
.container {
  width: 100vw;
  height: 100vh;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 18rem;
  padding: 0;
  align-items: center;
}
.img {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.img img {
  width: 500px;
}

.login-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}
form {
  width: 360px;
}
.avatar {
  width: 100px;
}
.login-box h2 {
  font-size: 2.9rem;
  text-transform: uppercase;
  margin: 15px 0;
  color: #999;
}

.input-group {
  position: relative;
  display: grid;
  grid-template-columns: 7% 93%;
  margin: 25px 0;
  padding: 5px 0;
  border-bottom: 2px solid #d9d9d9;
}

.input-group:nth-child(1) {
  margin-bottom: 4px;
}
.input-group:before,
.input-group:after {
  content: "";
  position: absolute;
  bottom: -2px;
  width: 0;
  height: 2px;
  background-color: #38d39f;
  transition: 0.5s;
}
.input-group:after {
  right: 50%;
}
.input-group:before {
  left: 50%;
}
.icon {
  display: flex;
  justify-content: center;
  align-items: center;
}
.icon i {
  color: #d9d9d9;
  transition: 0.5s;
}

.input-group > div {
  position: relative;
  height: 45px;
}

.input-group > div > h5 {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #d9d9d9;
  font-size: 18px;
  transition: 0.3s;
  padding: 0;
  margin: 0;
}
.input-group.focus .icon i {
  color: #38d39f;
}
.input-group.focus div h5 {
  top: -5px;
  font-size: 15px;
}
.login_wrap :deep(.input-group .nolabel .el-form-item__label) {
  display: none;
}
.login_wrap :deep(.input-group .nolabel input:-internal-autofill-previewed),
.login_wrap :deep(.input-group .nolabel input:-internal-autofill-selected) {
  /* -webkit-text-fill-color: #ffffff !important; */
  /* transition: background-color 5000s ease-in-out 0s !important; */
}
.login_wrap :deep(.input-group .nolabel input:-webkit-autofill) {
 box-shadow: 0 0 0px 1000px #fff inset !important;
}

.login_wrap :deep(.input-group .nolabel) {
  width: 100%;
  height: 100%;
}
.login_wrap :deep(.input-group .nolabel .el-form-item__content .el-input) {
  height: 100%;
}
.login_wrap
  :deep(
    .input-group .nolabel .el-form-item__content .el-input .el-input__wrapper
  ) {
  padding: 0;
  box-shadow: none !important;
}
.login_wrap
  :deep(.input-group .nolabel .el-form-item__content .el-form-item__error) {
  top: 114%;
}
.input-group.focus:after,
.input-group.focus:before {
  width: 50%;
}
.login_wrap :deep(.input-group .nolabel .el-input__wrapper) {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  border: none;
  outline: none;
  background: none;
  padding: 0.5rem 0.7rem;
  font-size: 1.2rem;
  color: #555;
  font-family: "Roboto", sans-serif;
}
a {
  display: block;
  text-align: right;
  text-decoration: none;
  color: #999;
  font-size: 0.9rem;
  transition: 0.3s;
}
a:hover {
  color: #38d39f;
}
.btn {
  display: block;
  width: 100%;
  height: 50px;
  border-radius: 25px;
  margin: 1rem 0;
  font-size: 1.2rem;
  outline: none;
  border: none;
  background-image: linear-gradient(to right, #32be8f, #38d39f, #32be8f);
  cursor: pointer;
  color: #fff;
  text-transform: uppercase;
  font-family: "Roboto", sans-serif;
  background-size: 200%;
  transition: 0.5s;
}
.btn:hover {
  background-position: right;
}
.copyright {
  position: absolute;
  width: 100%;
  height: 50px;
  bottom: 2px;
  color: #38d39f;
  text-align: center;
  font-size: 18px;
  font-family: "Roboto", sans-serif;
}

/*媒体查询*/
@media screen and (max-width: 1080px) {
  .container {
    grid-gap: 9rem;
  }
}
@media screen and (max-width: 1024px) {
  form {
    width: 290px;
  }
  form h2 {
    font-size: 2.4rem;
    margin: 8px 0;
  }
  .img img {
    width: 360px;
  }
}
@media screen and (max-width: 768px) {
  .wave {
    display: none;
  }
  .img {
    display: none;
  }
  .container {
    grid-template-columns: 1fr;
  }
  .login-box {
    justify-content: center;
  }
}
</style>
